package com.blast.manager.net.presenter

import android.text.TextUtils
import android.widget.TextView
import androidx.lifecycle.Observer
import com.wxl.common.base.AbsActivity
import com.wxl.common.bean.BatteryBean
import com.wxl.common.life.LifecycleManager
import com.wxl.common.viewmodel.AbsPresenter

class NetCheckChargePresenter : AbsPresenter() {

    lateinit var netCheckElectricText: TextView
    lateinit var netCheckChargeText: TextView
    override fun onCreated(vararg args: Any) {
        netCheckChargeText = args[0] as TextView
        netCheckElectricText = args[1] as TextView
        val bb = BatteryBean()
        bb.observe(netCheckChargeText.context as AbsActivity, Observer {
            it.current?.let { it1 -> netCheckChargeText.setText("电压:${it1/10.0}V") }
        })
        LifecycleManager.manager.registerLiveData(
            bb,
            (netCheckChargeText.context as AbsActivity).lifecycle
        )
    }

    fun onChargeElectricChanged(mVoltage: Int, mElectric: Int) {
        netCheckChargeText.text = "电压:${mVoltage/10.0}V"
        netCheckElectricText.text = "电流:${mElectric/10.0}mA"
    }

    fun getVoltage():Double{
        if(TextUtils.isEmpty(netCheckChargeText.text))return 0.0
        val v = netCheckChargeText.text.toString()
            .replace("电压:","")
            .replace("V","")
            .trim()
        return v.toDouble()*10
    }

    fun getElectric():Double{
        if(TextUtils.isEmpty(netCheckElectricText.text))return 0.0
        val mA = netCheckElectricText.text.toString()
            .replace("电流:","")
            .replace("mA","")
            .trim()
        return mA.toDouble()*10
    }
}